from Tkinter import *
from tkMessageBox import *
from timer import Timer
import db
from datetime import datetime

log = db.Log()
	
def start():
	Timer.stop()
	timer = Timer(timer_label.tick)
	timer.start()
	button_start.config(text='Pause timer', command=pause)
	
def pause():
	Timer.stop()
	button_start.config(text='Start timer', command=start)
	log.add(created=datetime.now(), hours=Timer.hours, minutes=Timer.minutes, seconds=Timer.seconds,
		comment='Paused at '+datetime.now().isoformat(' '))
	
def zero():
	Timer.stop()
	log.add(created=datetime.now(), hours=Timer.hours, minutes=Timer.minutes, seconds=Timer.seconds,
		comment='Cleared at '+datetime.now().isoformat(' '))
	Timer.zero()
	timer_label.zero()
	button_start.config(text='Start timer', command=start)
			
def close():
	if askyesno('Big question', 'Really want to quit?'):
		Timer.stop()
		log.add(created=datetime.now(), hours=Timer.hours, minutes=Timer.minutes, seconds=Timer.seconds,
			comment='Program exit at '+datetime.now().isoformat(' '))
		sys.exit()
		
def makemenu(window):
	menu = Menu(window)
	window.config(menu=menu)
	
	menu_timer = Menu(menu)
	menu_timer.add_command(label='Zero timer', command=zero)
	menu_timer.add_command(label='Set timer time', command=lambda: 0)
	menu_timer.add_command(label='Close', command=close)
	menu.add_cascade(label='Timer', menu=menu_timer)
	
	menu_reports = Menu(menu)
	menu_reports.add_command(label='Full log', command=lambda: 0)
	menu_reports.add_command(label='Month', command=lambda: 0)
	menu_reports.add_command(label='Today', command=lambda: 0)
	menu.add_cascade(label='Reports', menu=menu_reports)
	
	menu_cloud = Menu(menu)
	menu_cloud.add_command(label='Sync', command=lambda: 0)
	menu.add_cascade(label='Cloud', menu=menu_cloud)
	
class TimerLabel(Label):
	"""Timer view"""
	def __init__(self, parent):
		Label.__init__(self, parent)
		self.config(text="00:00:00", height=3, width=14, font=('Monospaced', 38), bg='black', fg='green')
		self.pack(side=TOP, pady=8)
		
	def tick(self, hours, minutes, seconds):
		self['text'] = "%02d:%02d:%02d" % (hours, minutes, seconds)
		
	def zero(self):
		self['text'] = "00:00:00"

root = Tk()
root.title('Work Timer')
root.geometry('-100+100')
root.protocol('WM_DELETE_WINDOW', close)
makemenu(root)

timer_label = TimerLabel(root)

button_start = Button(root, text="Start timer", command=start, width=50)
button_start.pack(side=LEFT)

button_close = Button(root, text="Close", command=close)
button_close.pack(side=RIGHT)

root.mainloop()
